package com.sfx.concurrent.demo.concurrent.countDownLatch;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-08-12
 * Time: 13:48
 */
@Slf4j(topic = "c.TestCountDownLatch1")
public class TestCountDownLatch1 {
    private static CountDownLatch latch = new CountDownLatch(10);
    private static ExecutorService pool = Executors.newFixedThreadPool(10);
    private static Random random = new Random();

    public static void main(String[] args) throws InterruptedException {
        String[] str = new String[10];
        for (int j = 0; j < 10; ++j) {
            int k = j;
            pool.submit(() -> {
                for (int i = 0; i <= 100; ++i) {
                    try {
                        Thread.sleep(random.nextInt(100));
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    str[k] = i + "%";
                    System.out.print("\r" + Arrays.toString(str));
                }
                latch.countDown();
            });
        }

        latch.await();
        pool.shutdownNow();
        System.out.println();
        System.out.print("\r" + "游戏开始");
    }
}
